package com.cxq.finalproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.cxq.finalproject.databinding.ActivityRegisterBinding;
import com.cxq.finalproject.util.MyUserHelper;

public class RegisterActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    //声明bing
    private ActivityRegisterBinding bing;
    //    数据库操作类
    MyUserHelper myUserHelper;
    private String sex = "男";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        bing = DataBindingUtil.setContentView(this,R.layout.activity_register);
        // 打开数据表库表
        myUserHelper = new MyUserHelper(this, MyUserHelper.DB_NAME, null, 1);
        bing.radioGroup2.setOnCheckedChangeListener(this);

        bing.button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(bing.editTextTextPersonName3.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "姓名不能为空!",
                            Toast.LENGTH_SHORT).show();
                }else if (bing.editTextTextPersonName2.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "用户名不能为空！",
                            Toast.LENGTH_SHORT).show();
                } else if (bing.editTextTextPassword2.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "密码不能为空！",
                            Toast.LENGTH_SHORT).show();
                } else if (!bing.editTextTextPassword2.getText().toString().equals(
                        bing.editTextTextPassword3.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "密码不正确",
                            Toast.LENGTH_SHORT).show();
                }else{
                    long rid = regStudent();
                    if (rid != -1) {
                        Toast.makeText(getApplicationContext(), "注册成功",
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "用户已经存在",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public long regStudent(){
        SQLiteDatabase db = myUserHelper.getWritableDatabase(); // 获得数据库对象
        ContentValues values = new ContentValues();
        values.put(MyUserHelper.ID, bing.getUserId() );
        values.put(MyUserHelper.PWD, bing.getPassword() );
        values.put(MyUserHelper.UserNAME,bing.editTextTextPersonName3.getText().toString() );
        values.put(MyUserHelper.GANDER, sex );
        // 插入数据
        long rid = db.insert(MyUserHelper.TABLE_NAME, MyUserHelper.ID, values);
        db.close();
        return rid;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        RadioButton radio = (RadioButton) findViewById(checkedId);
        if(checkedId == 1000005 ){
            sex = "女";
        }else{
            sex = "男";
        }

    }
}